package org.bee.service.ext;

import org.bee.ResultTool;
import org.bee.cryptix.BCryptUtils;
import org.bee.enums.IsDelete;
import org.bee.enums.ResultCode;
import org.bee.enums.UserStatus;
import org.bee.input.LoginVo;
import org.bee.input.UserSearchVo;
import org.bee.mappers.AdminUserMapper;
import org.bee.model.AdminUser;
import org.bee.model.AdminUserExample;
import org.bee.model.result.Result;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminUserExtService {

    AdminUserMapper adminUserMapper;
    public AdminUserExtService(AdminUserMapper adminUserMapper) {
        this.adminUserMapper = adminUserMapper;
    }

    /**
     * 根据用户名查询用户
     * @param userName 用户名
     * @return AdminUser 用户信息
     */
    public List<AdminUser> getUserByUserName(String userName){
        AdminUserExample example = new AdminUserExample();
        AdminUserExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(userName);
        criteria.andIsDeletedEqualTo(Boolean.valueOf(IsDelete.FALSE.getCode()));
        return adminUserMapper.selectByExample(example);
    }

    /**
     * 登录检查
     * @param user 登录输入的账号密码等信息
     * @param adminUser 用户信息
     * @return 校验结果
     */
    public Result loginCheck(LoginVo user, AdminUser adminUser){
        Result result = ResultTool.success();
        //用户状态检查
        if (adminUser.getStatus().equals(UserStatus.FREEZE.getCode())) {
            result = ResultTool.fail(ResultCode.USER_ACCOUNT_FREEZE);
            return result;
        }
        //密码校验
        if(!BCryptUtils.checkPW(user.getPassword(),adminUser.getPassword())){
            result = ResultTool.fail(ResultCode.USER_CREDENTIALS_ERROR);
            return result;
        }
        return result;
    }

    public List<AdminUser> getUserList(UserSearchVo userSearchVo) {
        AdminUserExample example = new AdminUserExample();
        AdminUserExample.Criteria criteria = example.createCriteria();
        AdminUserExample.Criteria  nicknameCriteria= example.or();
        AdminUserExample.Criteria  phoneCriteria= example.or();
        criteria.andIsDeletedEqualTo(Boolean.valueOf(IsDelete.FALSE.getCode()));
        if(userSearchVo.getDepartmentId() != null && !"".equals(userSearchVo.getDepartmentId())){
            criteria.andDepartmentIdEqualTo(userSearchVo.getDepartmentId());
        }
        if(userSearchVo.getKeywords() != null && !"".equals(userSearchVo.getKeywords())){
            criteria.andUsernameLike("%"+userSearchVo.getKeywords()+"%");
            nicknameCriteria.andNicknameLike("%"+userSearchVo.getKeywords()+"%");
            phoneCriteria.andPhoneLike("%" + userSearchVo.getKeywords()+"%");
        }else{
            if(userSearchVo.getUsername() != null && !"".equals(userSearchVo.getUsername())){
                criteria.andUsernameLike("%"+userSearchVo.getUsername()+"%");
            }
            if (userSearchVo.getNickname() != null && !"".equals(userSearchVo.getNickname())){
                criteria.andNicknameLike("%"+userSearchVo.getNickname()+"%");
            }
        }
        example.setOrderByClause("update_time desc");
        return adminUserMapper.selectByExample(example);
    }
}
